//
//  UIViewController+BarButtonItem.m
//  TravelGuideMdd
//
//  Created by Song Xiaofeng on 13-7-2.
//  Copyright (c) 2013年 mafengwo.com. All rights reserved.
//

#import "UIViewController+BarButtonItem.h"

@implementation UIViewController (BarButtonItem)
#pragma mark - 设置NavigationController的左右按钮

- (void)setControllerTitle:(NSString *)title{
    id titleView = self.navigationItem.titleView;
    if ([titleView isKindOfClass:[UILabel class]]){
        ((UILabel *)titleView).text = title;
    }else{
        UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 180, 40)];
        titleLabel.text = title;
        titleLabel.fontSize = 17;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = [UIColor colorWithARGB:0xffffff];
        self.navigationItem.titleView = titleLabel;
    }
}

- (void)setBarButtonWithNormalImgName:(NSString *)normalImgStr
                       highlightedImg:(NSString *)highlightedImgStr
                               target:(id)target action:(SEL)action
                                 type:(NavigationBarItemType)type
{
        [self setBarButtonWithNormalImg:[UIImage imageNamed:normalImgStr]
                         highlightedImg:[UIImage imageNamed:highlightedImgStr]
                                 target:target
                                 action:action
                                   type:type];
}

-(void)setBarButtonWithNormalImg:(UIImage*)normalImgStr
                  highlightedImg:(NSString*)highlightedImgStr
                          target:(id)target
                          action:(SEL)action
                            type:(NavigationBarItemType)type
{
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.exclusiveTouch = YES;
    
    UIImage* normalImg = normalImgStr;
    [button setImage:normalImg forState:UIControlStateNormal];
    
    UIImage* highlightedImg = [UIImage imageNamed:highlightedImgStr];
    [button setImage:highlightedImg forState:UIControlStateHighlighted];
    
    [button sizeToFit];
    
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    if (type == NavigationBarItemTypeLeft) {
        self.navigationItem.leftBarButtonItem = buttonItem;
    }
    else
    {
        self.navigationItem.rightBarButtonItem = buttonItem;
    }
}

- (void)setBarButtonWithNormalImgName:(NSString *)normalImgStr
                       highlightedImg:(NSString *)highlightedImgStr
                          disabledImg:(NSString *)disabledImgStr
                               target:(id)target action:(SEL)action
                                 type:(NavigationBarItemType)type
{
    [self setBarButtonWithNormalImg:[UIImage imageNamed:normalImgStr]
                     highlightedImg:[UIImage imageNamed:highlightedImgStr]
                        disabledImg:[UIImage imageNamed:disabledImgStr]
                             target:target
                             action:action
                               type:type];
}

-(void)setBarButtonWithNormalImg:(UIImage*)normalImgStr
                  highlightedImg:(UIImage*)highlightedImgStr
                     disabledImg:(UIImage*)disabledImgStr
                          target:(id)target
                          action:(SEL)action
                            type:(NavigationBarItemType)type;
{
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    UIImage* normalImg = normalImgStr;
    [button setImage:normalImg forState:UIControlStateNormal];
    
    UIImage* highlightedImg = highlightedImgStr;
    [button setImage:highlightedImg forState:UIControlStateHighlighted];
    
    UIImage *disabledImg = disabledImgStr;
    [button setImage:disabledImg forState:UIControlStateDisabled];
    
    [button sizeToFit];
    
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    if (type == NavigationBarItemTypeLeft) {
        self.navigationItem.leftBarButtonItem = buttonItem;
    }
    else
    {
        self.navigationItem.rightBarButtonItem = buttonItem;
    }
}
@end
